﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrossPlatform;

namespace ConfigurationControls
{
    public partial class ConfigurationMain : UserControl
    {
        ConfigurationDataSet configurationData = null;
        Common.NetControlToolsDataSet netControlDataset = new Common.NetControlToolsDataSet();
        public ConfigurationMain()
        {
            InitializeComponent();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (configurationData == null)
            {
                configurationData = createDefaultConfigurationDataset();
                System.Diagnostics.Debug.WriteLine( configurationData.Tables["Server"].Rows.Count.ToString());
            }

            try
            {
                if (configurationData != null)
                {
                    configurationData.WriteXml(
                        CrossPlatform.CrossPlatformConstants.NetControlToolsConfigurationDatabaseFilename,
                        XmlWriteMode.WriteSchema);
                }
            }
            catch (System.IO.DirectoryNotFoundException directoryNotFoundException)
            {
                MessageBox.Show(String.Format(
                    "The required directory <<{0}>> was not found.  I will attempt to create it.  Please try again.",
                    System.IO.Path.GetDirectoryName(
                    CrossPlatform.CrossPlatformConstants.NetControlToolsConfigurationDatabaseFilename)));
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(
                    CrossPlatform.CrossPlatformConstants.NetControlToolsConfigurationDatabaseFilename));

            }
            catch (Exception)
            {

                throw;
            }
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            if (configurationData == null)
            {
                configurationData = createDefaultConfigurationDataset();
            }
            try
            {
                configurationData.ReadXml(
                    CrossPlatform.CrossPlatformConstants.NetControlToolsConfigurationDatabaseFilename,
                    XmlReadMode.ReadSchema);
            }
            catch (Exception)
            {

                throw;
            }
        }

        private ConfigurationDataSet createDefaultConfigurationDataset()
        {
            ConfigurationDataSet defaultConfigurationDataset = new ConfigurationDataSet();
            return defaultConfigurationDataset;
        }

        private void ConfigurationMain_Load(object sender, EventArgs e)
        {
            loadConfigurationFromDatabase();
        }

        private void loadConfigurationFromDatabase()
        {
            Common.NetControlToolsDataSetTableAdapters.SensorTableAdapter sensorTableAdapter =
                new Common.NetControlToolsDataSetTableAdapters.SensorTableAdapter();
            Common.NetControlToolsDataSet.SensorDataTable sensorDataTable =
                new Common.NetControlToolsDataSet.SensorDataTable();
            sensorTableAdapter.Fill(sensorDataTable);
        }
    }
}
